//
//  ConfirmPriceCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/2.
//

import UIKit

class ConfirmPriceCell: UITableViewCell {
    var reduceData: CouponDataModel?
    var orderData: PreviewProductOrderDataModel?
    var bkgView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = .white
        tempView.layer.cornerRadius = 8
        tempView.layer.masksToBounds = true
        return tempView
    }()
    
    var tableView: BaseTableView!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = Theme.viewBkgGrayColor
        contentView.addSubview(bkgView)
        
        tableView = BaseTableView.init(frame: CGRect.zero, style: .plain)
        tableView.register(ConfirmPriceSubCell.self, forCellReuseIdentifier: "ConfirmPriceSubCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.contentInset = UIEdgeInsets.zero
        tableView.estimatedRowHeight = 40
        tableView.rowHeight = UITableViewAutomaticDimension
        bkgView.addSubview(tableView)
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.width.equalTo(kScreenWidth - 28)
            make.top.equalTo(10)
            make.bottom.equalTo(0).priority(.high)
        }
        
        self.tableView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(10)
            make.height.equalTo(80)
            make.bottom.equalTo(bkgView.snp.bottom).offset(-10)
        }
    }
    
    func setConfirmPriceCell() {
        self.tableView.frame.size.height = 999
        self.tableView.reloadData()
        self.tableView.layoutSubviews()
        self.tableView.layoutIfNeeded()
        
        let tempH = self.tableView.contentSize.height
        self.tableView.snp.updateConstraints { make in
            make.height.equalTo(tempH)
        }
        self.updateConstraints()
        self.layoutIfNeeded()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ConfirmPriceCell: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        //判断是否展示优惠
        if indexPath.row == 0 && (self.reduceData?.reducePrice ?? 0) == 0 {
            //说明有优惠
            return 0.01
        }
        return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ConfirmPriceSubCell", for: indexPath) as! ConfirmPriceSubCell
        switch indexPath.row {
            case 0:
                let reducePrice = XTManageCenter.showPriceInComma(source: (self.reduceData?.reducePrice ?? 0).cleanZero)
                cell.titleLab.text = MMLocalizedString("confirm_reduce", comment: "优惠")
                cell.rightLab.text = String(format: "-%@%@", XTManageCenter.currentSign(), reducePrice)
            case 1:
                cell.titleLab.text = MMLocalizedString("confirm_real_pay", comment: "实付金额")
                let totalPrice = self.orderData?.price ?? 0
                let couponPrice = self.reduceData?.reducePrice ?? 0
                let lastPrice = XTManageCenter.showPriceInComma(source: (max(0, totalPrice - couponPrice)).cleanZero)
                cell.rightLab.text = String(format: "%@%@", XTManageCenter.currentSign(), lastPrice)
            default:
                ()
        }
        cell.updateConstraints()
        cell.selectionStyle = .none
        cell.clipsToBounds = true
        
        return cell
    }
}


